//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using System.Xml;
using CommunityServer.Configuration;
using System.Collections.Specialized;

namespace CommunityServer.Components
{
	public class BrowseableLists
	{
		private static ArrayList _browseableLists;

		static BrowseableLists()
		{
			_browseableLists = new ArrayList();

			XmlNode node = CSConfiguration.GetConfig().GetConfigSection("CommunityServer/BrowseableLists");
			if (node != null)
			{
				foreach(XmlNode n in node.ChildNodes)
				{
					if(n.NodeType != XmlNodeType.Comment)
					{
						if(n.Name == "add")
						{
							string itemType = n.Attributes["type"].Value;
							Type t = Type.GetType(itemType);
							if(t == null)
								throw new Exception(itemType);
                        
							IBrowseableList bl = Activator.CreateInstance(Type.GetType(itemType)) as IBrowseableList;
							if(bl != null)
								_browseableLists.Add(bl);
						}
					}
				}
			}
		}

		public static ArrayList Lists
		{
			get
			{
				return _browseableLists;
			}
		}
	}

	public class BrowseableListItemOptionSelectableValue
	{
		private string _value;
		private string _name;

		public string Value
		{
			get { return _value; }
		}

		public string Name
		{
			get { return _name; }
		}

		public BrowseableListItemOptionSelectableValue(string value, string name)
		{
			_value = value;
			_name = name;
		}
	}

	public enum BrowseableListItemOptionType
	{
		DateTime,
		Int,
		Float,
		String
	}

	public class BrowseableListItemOption
	{
		private string _fieldID;
		private string _fieldName;
		private BrowseableListItemOptionType _valueType;
		private ArrayList _selectableOptions;

		public string FieldID
		{
			get { return _fieldID; }
		}
		public string FieldName
		{
			get { return _fieldName; }
		}
		public BrowseableListItemOptionType ValueType
		{
			get { return _valueType; }
		}
		public ArrayList SelectableOptions
		{
			get { return _selectableOptions; }
		}
		public bool HasSelectableOptions
		{
			get { return SelectableOptions != null && SelectableOptions.Count > 0; }
		}

		public BrowseableListItemOption(string fieldID, string fieldName, BrowseableListItemOptionType valueType)
		{
			_fieldID = fieldID;
			_fieldName = fieldName;
			_valueType = valueType;
			_selectableOptions = null;
		}

		public BrowseableListItemOption(string fieldID, string fieldName, BrowseableListItemOptionType valueType, ArrayList selectableOptions)
		{
			_fieldID = fieldID;
			_fieldName = fieldName;
			_valueType = valueType;
			_selectableOptions = selectableOptions;
		}
	}

	/// <summary>
	/// Root node of a set of includable items
	/// </summary>
	public interface IBrowseableList
	{
		/// <summary>
		/// Root item displayed in the tree for this list
		/// </summary>
		string Name { get;}

		/// <summary>
		/// IBrowseableListItems to be displayed in the root
		/// </summary>
		ArrayList Items { get; }
		
		/// <summary>
		/// IBrowseableListContainers to be displayed in the root
		/// </summary>
		ArrayList Containers { get; }
	
		/// <summary>
		/// URL used to add an item to the root node (can be null)
		/// </summary>
        HyperLink AddItemUrl { get; }

		/// <summary>
		/// Retrieves the node identified by the list key
		/// </summary>
		/// <param name="listKey"></param>
		/// <returns></returns>
		object GetNode(string listKey);	
	
		/// <summary>
		/// Retrieves an arraylist of BrowseableListOptions applicable to this IBrowseableList
		/// </summary>
		/// <returns></returns>
		ArrayList GetBrowseableListOptions();

		/// <summary>
		/// Retrieves an StringCollection of listKeys representing the path from the root of this IBrowseableList to the specified listKey
		/// </summary>
		/// <param name="listKey"></param>
		/// <returns></returns>
		StringCollection GetPathToNode(string listKey);

		bool Enabled { get; }
	}

	/// <summary>
	/// Represents a container of includable items
	/// </summary>
	public interface IBrowseableListContainer
	{
		/// <summary>
		/// Unique identification key used to identify this container
		/// </summary>
		string ListKey { get; }

		/// <summary>
		/// Display name for this container
		/// </summary>
		string Name { get; }
	
		/// <summary>
		/// IBrowseableListItems to be displayed in this container
		/// </summary>
		ArrayList Items { get; }
		
		/// <summary>
		/// IBrowseableListContainers to be displayed in this container
		/// <summary>
		ArrayList Containers { get; }
	
		/// <summary>
		/// URL used to add an item to this container (can be null)
		/// </summary>
        HyperLink AddItemUrl { get; }
        
	    /// <summary>
        /// Validates the user can add items to this container
        /// </summary>
        /// <returns></returns>
        bool UserCanPost(User user);
	}

	/// <summary>
	/// Represents an includable item
	/// </summary>
	public interface IBrowseableListItem
	{
		/// <summary>
		/// Unique identification key used to identify this item
		/// </summary>
		string ListKey { get; }

		/// <summary>
		/// Name of this item
		/// </summary>
		string Name { get; }

		/// <summary>
		/// HTML used to preview this item
		/// </summary>
		string PreviewHTML { get; }
	
		/// <summary>
		/// HTML used to render this item
		/// </summary>
		/// <param name="optionValues">NameValueCollection containing values for BrowseableOptions</param>
		/// <returns></returns>
		string GetRenderHTML(NameValueCollection optionValues);

		string GetRenderHTML();
	}
}
